package com.beiming.moudle.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiming.common.utils.Result;
import com.beiming.moudle.auth.entity.ShopCarEntity;
import com.beiming.moudle.auth.entity.utillClass.ShopCarPage;
import com.beiming.moudle.auth.service.BussService;
import com.beiming.moudle.auth.service.ProInfoService;
import com.beiming.moudle.auth.service.ShopCarService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/shopcar")
public class ShopCarController {

    private final ShopCarService shopCarService;
    private final ProInfoService proInfoService;
    private final BussService bussService;
    public ShopCarController(ShopCarService shopCarService, ProInfoService proInfoService, BussService bussService) {
        this.shopCarService = shopCarService;
        this.proInfoService = proInfoService;
        this.bussService = bussService;
    }

    @PostMapping("/setShopCar")
    public Result setShopCar(@RequestBody ShopCarEntity shopCarEntity){
        System.out.println("++++++++"+shopCarEntity.toString());
        return Result.ok(shopCarService.addShopCar(shopCarEntity));
    }
    @PostMapping("/getMsg")
    public Result getMsgPage(@RequestBody ShopCarPage pa){
        System.out.println("++++++++++++"+pa);
        IPage<ShopCarEntity> bussPage = shopCarService.getMsgPage(pa);
        System.out.println(bussPage.toString());
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( pa.getCurrentPage() > bussPage.getPages()){
            bussPage = shopCarService.getMsgPage(pa);
        }
        List<ShopCarEntity> shopCarEntities = bussPage.getRecords();
        for(ShopCarEntity shopCarEntity : shopCarEntities){
            shopCarEntity.setProductName(proInfoService.getProduct(shopCarEntity.getProductId()).getProductName());
            shopCarEntity.setBussinessName(bussService.getBussUser(pa.getBussinessId()));
            shopCarEntity.setPrice(proInfoService.getProduct(shopCarEntity.getProductId()).getPrice());
        }
        return Result.ok(bussPage);
    }
    @PostMapping("/updateShopCar")
    public Result updateShopCar(@RequestBody ShopCarEntity shopCarEntity){
        System.out.println("=============="+shopCarEntity.toString());
        shopCarService.updateShopCar(shopCarEntity);
        return Result.ok();
    }
    @PostMapping("/deleteShopCar")
    public Result deleteShopCar(@RequestBody ShopCarEntity shopCarEntity){
        System.out.println(shopCarEntity.toString());
        shopCarService.deleteShopCar(shopCarEntity.getCartId());
        return Result.ok();
    }

    @PostMapping("/deleteAnyShopCar")
    public Result deleteAnyShopCar(@RequestBody ShopCarEntity shopCarEntity){

        shopCarService.deleteAnyShopCar(shopCarEntity.getCustomerId());
        return Result.ok();
    }
}
